package com.woniuxy.oa.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.oa.entity.Goods_purchase;
import com.woniuxy.oa.entity.PageBean;
import com.woniuxy.oa.enums.ApprovalApplyType;
import com.woniuxy.oa.exception.Goods_purchaseException;
import com.woniuxy.oa.service.EmployeeService;
import com.woniuxy.oa.service.GoodsRepositoryService;
import com.woniuxy.oa.service.Goods_purchaseService;
import com.woniuxy.oa.utils.ApprovalUtil;

@Controller
public class Goods_purchaseController {

	@Autowired
	Goods_purchaseService goods_purchaseServiceImpl;
	@Autowired
	GoodsRepositoryService goodsRepositoryService;
	@Autowired
	EmployeeService employeeService;

	// 添加申购信息
	@RequestMapping("/addGoods_purchase")
	@Transactional(rollbackFor = { RuntimeException.class, Error.class, Exception.class })
	public String addPurchaseGetGp_id(Goods_purchase goods_purchase,Integer a_eid, Model model,HttpServletRequest request) {
		//模拟从session里获取id
		goods_purchase.setId(4);
		System.out.println(-1);
		// 验证员工id
		//这个id是前端从session域里获取的
//		Integer id=goods_claim.getId();
//		Employee employee = (Employee) request.getSession().getAttribute("employee");
//		Integer sessionid = employee.getId();
//		if(sessionid==null) {
//			 model.addAttribute("msg", "没有权利添加信息");
//			 return "msg";
//		}
//		Integer sessionId =(Integer) sessionid;
//		if(id != sessionId) {
//			 model.addAttribute("msg", "没有权利添加此人的信息");
//			 return "msg";
//		}
			 
			 
//		Integer id = goods_purchase.getId();
//		if (id == null) {
//			model.addAttribute("id", "员工编号不能为空");
//			return "addGoods_purchase";
//		} else if (employeeService.getEmployeeGetId(id) == null) {
//			model.addAttribute("id", "该员工编号不存在");
//			return "addGoods_purchase";
//		}
		
		//验证审批人的id不为空
		  if (a_eid == null) { 
			  model.addAttribute("a_eid", "审批人不能为空"); 
			  return "addGoods_purchase";
		  }
		
		
		// 验证物品名称
		String gp_name = goods_purchase.getGp_name();
		if (gp_name == null || gp_name.trim().equals("")) {
			model.addAttribute("gp_name", "物品名称不能为空");
			return "addGoods_purchase";
		}
		
		// 验证单价
				Double gp_dprice = goods_purchase.getGp_dprice();
				if (gp_dprice == null) {
					model.addAttribute("gp_dprice", "单价不能为空");
					return "addGoods_purchase";
				} else if (gp_dprice <= 0) {
					model.addAttribute("gp_dprice", "单价不能为0和负数");
					return "addGoods_purchase";
				}
		
		// 验证数量
		Integer gp_number = goods_purchase.getGp_number();
		if (gp_number == null) {
			model.addAttribute("gp_number", "数量不能为空");
			return "addGoods_purchase";
		} else if (gp_number < 1) {
			model.addAttribute("gp_number", "购买的数量不可以小于1");
			return "addGoods_purchase";
		}

		// 验证日期
		String date = goods_purchase.getGp_time();
		if (date == null || date.equals("")) {
			model.addAttribute("gp_time", "日期不能为空");
			return "addGoods_purchase";
		}
		// 验证状态
//		Integer gp_status = goods_purchase.getGp_status();
//		if (gp_status == null) {
//			model.addAttribute("gp_status", "状态不能为空");
//			return "addGoods_purchase";
//		} else if (goods_purchase.getGp_status() != 0 && goods_purchase.getGp_status() != 1) {
//			model.addAttribute("gp_status", "审批状态只能是0或1，0代表未审批，1代表已审批");
//			return "addGoods_purchase";
//		}

		try {
			System.out.println(goods_purchase);
			//获取传过来对象的单价和数量，计算总价格
			gp_number=goods_purchase.getGp_number();
			gp_dprice = goods_purchase.getGp_dprice();
			Double gp_zprice=gp_number*gp_dprice;
			goods_purchase.setGp_zprice(gp_zprice);
			System.out.println(gp_zprice);
			//往申领表里插入数据
			System.out.println(0);
			goods_purchaseServiceImpl.addPurchaseGetGp_id(goods_purchase);
			//从枚举类里获取审批类型
			System.out.println(1);
			ApprovalApplyType a_type = ApprovalApplyType.APPLYBUY;
			System.out.println(2);
			//从前端下拉框获取审批人的id，插入审批表，并返回审批号
			Integer a_id = ApprovalUtil.addApprovalGetId(request, a_eid, a_type);
			System.out.println(3);
			//获取插入申购申请表后的主键
			Integer table_key = goods_purchase.getGp_id();
			System.out.println(4);
			//向关系表插入申请表的主键、审批表的审批号（主键）、审批类型
			ApprovalUtil.addRelationship(table_key, a_id, a_type);
			System.out.println(5);
			System.out.println("添加成功");
			return "system/officeSubscribe";
		} catch (Goods_purchaseException e) {
			e.printStackTrace();
			model.addAttribute("msg", "系统维护中");
			return "msg";
		}
	}

	// 删除信息：用软删除
	@ResponseBody
	@RequestMapping(value="/removeGoods_purchase",produces = "application/json;charset=UTF-8")
	public Object removePuchaseByGp_id(Integer gp_id, Model model) {
		try {
			goods_purchaseServiceImpl.removePuchaseByGp_id(gp_id);
			return "删除成功";
		} catch (Goods_purchaseException e) {
			e.printStackTrace();
			model.addAttribute("msg", "系统维护中");
			return "删除失败";
		}
	}

	// 根据id查申购信息和员工姓名，修改是回显用
	@RequestMapping("/selectGoods_purchase/{gp_id}")
	public String selectGoods_purchaseByGp_id(@PathVariable("gp_id") Integer gp_id, Model model) {
		try {
			Goods_purchase goods_purchase = goods_purchaseServiceImpl.selectGoods_purchaseByGp_id(gp_id);
			System.out.println(goods_purchase);
			if(goods_purchase.getGp_status()==2) {
				return "msg";
			}
			
			model.addAttribute("goods_purchase", goods_purchase);
			return "updateGoods_purchase";
		} catch (Goods_purchaseException e) {
			e.printStackTrace();
			model.addAttribute("msg", "系统维护中");
			return "msg";
		}
	}

	// 根据修改信息
	@RequestMapping("/modifyGoods_purchaseByGp_id")
	public String modifyGoods_purchaseByGc_id(Goods_purchase goods_purchase, Model model,HttpServletRequest request) {
		//验证员工id
//		Integer id=goods_purchase.getId();
//		Employee employee = (Employee) request.getSession().getAttribute("employee");
//		Integer sessionid = employee.getId();
//		if(sessionid==null) {
//			model.addAttribute("msg", "没有权利修改信息");
//			return "msg";
//		}
//		Integer sessionId =(Integer) sessionid;
//		if(id != sessionId) {
//			model.addAttribute("msg", "没有权利修改此人的信息");
//			return "msg";
//		}

		// 验证物品名称
		String gp_name = goods_purchase.getGp_name();
		if (gp_name == null || gp_name.trim().equals("")) {
			model.addAttribute("gp_name", "物品名称不能为空");
			return "updateGoods_purchase";
		}
		
		// 验证单价
		Double gp_dprice = goods_purchase.getGp_dprice();
		if (gp_dprice == null) {
			model.addAttribute("gp_dprice", "单价不能为空");
			return "updateGoods_purchase";
		} else if (gp_dprice <= 0) {
			model.addAttribute("gp_dprice", "单价不能为0和负数");
			return "updateGoods_purchase";
		}
		// 验证数量
		Integer gp_number = goods_purchase.getGp_number();
		if (gp_number == null) {
			model.addAttribute("gp_number", "数量不能为空");
			return "updateGoods_purchase";
		} else if (gp_number < 1) {
			model.addAttribute("gp_number", "购买的数量不可以小于1");
			return "updateGoods_purchase";
		}
		
		// 验证总价
				Double gp_zprice = goods_purchase.getGp_zprice();
				if (gp_zprice == null) {
					model.addAttribute("gp_zprice", "总价不能为空");
					return "updateGoods_purchase";
				} else if (gp_zprice <= 0) {
					model.addAttribute("gp_zprice", "总价不能为0和负数");
					return "updateGoods_purchase";
				}
		
		// 验证日期
		String date = goods_purchase.getGp_time();
		if (date == null || date.equals("")) {
			model.addAttribute("gp_time", "日期不能为空");
			return "updateGoods_purchase";
		}
//		// 验证状态
//		Integer gp_status = goods_purchase.getGp_status();
//		if (gp_status == null) {
//			model.addAttribute("gp_status", "状态不能为空");
//			return "updateGoods_purchase";
//		} else if (goods_purchase.getGp_status() != 0 && goods_purchase.getGp_status() != 1) {
//			model.addAttribute("gp_status", "审批状态只能是0或1，0代表未审批，1代表已审批");
//			return "updateGoods_purchase";
//		}

		System.out.println("修改qian");
		try {
			goods_purchaseServiceImpl.modifyPurchaseByGp_id(goods_purchase);
			System.out.println("修改成功");
			return "system/officeSubscribe";
		} catch (Goods_purchaseException e) {
			e.printStackTrace();
			model.addAttribute("msg", "系统维护中");
			return "msg";
		}
	}

	// 按条件分页查询
	@ResponseBody
	@RequestMapping("/selectGoods_purchases")
	public PageBean<Goods_purchase> selectGoods_purchasesByConditionPage(HttpServletRequest request, String name) {
		PageBean<Goods_purchase> pb = null;
		try {
			Integer pageIndex = getPageIndex(request);
			Integer pageSize = 10;
			pb = goods_purchaseServiceImpl.getGoods_purchaseByPage(name, pageIndex, pageSize);
			return pb;
		} catch (Goods_purchaseException e) {
			e.printStackTrace();
			request.setAttribute("msg", "系统维护中");
			return pb;
		}

	}

	// 获取当前页
	private Integer getPageIndex(HttpServletRequest request) {
		if (request.getParameter("pageIndex") == null || request.getParameter("pageIndex").equals("")) {
			return 1;
		}
		return Integer.parseInt(request.getParameter("pageIndex"));
	}
}
